package com.example.weather_app.music;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_app.R;

import java.util.List;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder> {

    private List<Song> songList;
    private Context context;
    private OnSongClickListener listener;
    private int currentPlayingPosition = -1;

    public SongAdapter(List<Song> songList, Context context, OnSongClickListener listener) {
        this.songList = songList;
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public SongViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_song, parent, false);
        return new SongViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SongViewHolder holder, int position) {
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition != RecyclerView.NO_POSITION) {
            Song song = songList.get(adapterPosition);
            holder.tvTitle.setText(song.getTitle());
            holder.tvArtist.setText(song.getArtist());
            holder.tvDuration.setText(song.getDuration());

            // 设置当前播放歌曲的高亮效果
            if (adapterPosition == currentPlayingPosition) {
                holder.itemView.setBackgroundResource(R.drawable.bg_song_selected);
                holder.tvTitle.setTextColor(context.getResources().getColor(R.color.colorAccent));
            } else {
                holder.itemView.setBackgroundResource(android.R.color.transparent);
                holder.tvTitle.setTextColor(context.getResources().getColor(android.R.color.white));
            }

            // 点击事件等其他涉及到position的操作，也同样替换为adapterPosition
            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onSongClick(adapterPosition);
                    currentPlayingPosition = adapterPosition;
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }

    public void setCurrentPlayingPosition(int position) {
        this.currentPlayingPosition = position;
        notifyDataSetChanged();
    }

    class SongViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvArtist, tvDuration;
        ImageView ivPlayingIndicator;

        public SongViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_song_name);
            tvArtist = itemView.findViewById(R.id.tv_artist);
            tvDuration = itemView.findViewById(R.id.tv_total_time);
            ivPlayingIndicator = itemView.findViewById(R.id.iv_playing_indicator);
        }
    }

    public interface OnSongClickListener {
        void onSongClick(int position);
    }
}